package com.ellecity06.common.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.viewbinding.ViewBinding
import com.ellecity06.common.base.view.LoadUiIntent
import com.ellecity06.common.helper.LoginHelper
import com.ellecity06.common.utils.ToastUtils
import java.lang.reflect.ParameterizedType

/**
 * @author ellecity06
 * @time 2023/3/30 10:25
 * @des
 */
abstract class BaseMvvmFragment<VB : ViewBinding, out VM : BaseViewModel> : BaseFragment() {
    protected val viewModel: VM by lazy { initViewModel() }
    private var isLoaded = false

    private var _binding: VB? = null

    protected val binding get() = _binding!!
     open val isLazy = true
    override fun onResume() {
        super.onResume()
        if (!isLoaded && !isHidden&&isLazy) {
            lifecycleScope.launchWhenCreated {
                lazyInit()
            }
            isLoaded = true
        }
    }

    abstract fun lazyInit()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        _binding = inflateBinding(inflater, container)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initView()
        initBaseObserver()
        if (!isLazy) lazyInit()
    }

    private fun initBaseEvent() {
    }

    private fun initBaseObserver() {
        lifecycleScope.launchWhenStarted {
            viewModel.loadUiIntentFlow.collect {
                when (it) {
                    is LoadUiIntent.Error -> {
                        ToastUtils.show(it.msg)
                    }
                    is LoadUiIntent.Loading -> {
                        if (it.isShow) {
                            showLoading(text = it.text)
                        } else {
                            dismissLoading()
                        }
                    }

                    is LoadUiIntent.AuthorizationExpired -> {
                        onAuthorizationExpired(it.message)
                    }
                }
            }
        }
    }

    open fun onAuthorizationExpired(message: String) {
        ToastUtils.show(message)
        LoginHelper.instance.logout(requireActivity())

    }


    open fun initView() = Unit


    abstract fun inflateBinding(inflater: LayoutInflater, container: ViewGroup?): VB
    override fun onDestroyView() {
        isLoaded = false
        super.onDestroyView()
        _binding = null
    }
    open fun initViewModel(): VM {
        val type = javaClass.genericSuperclass
        return if (type is ParameterizedType) {
            val tp = type.actualTypeArguments[1]
            val tClass = tp as? Class<VM> ?: BaseViewModel::class.java
            ViewModelProvider(this)[tClass] as VM
        } else {
            ViewModelProvider(this)[BaseViewModel::class.java] as VM

        }
    }
}